<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('通道账户列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            账户：<input type="text" name="account"/>
                        </li>
                        <li>
                            支付类型：<select  id="payType"  name="payType">
                            <option value="">所有</option>
                            <option th:each="dict : ${payerPayTypes}" th:text="${dict.payLabel}" th:value="${dict.payType}"></option>
                        </select>
                        </li>
                        <li>
                            分组：<select  id="groupName"  name="groupName">
                            <option value="">所有</option>
                            <option th:each="dict : ${groups}" th:text="${dict.groupName}" th:value="${dict.groupName}"></option>
                        </select>
                        </li>
                        <li>
                            账户状态：<select name="status">
                            <option value="">所有</option>
                            <option value="0">正常</option>
                            <option value="1">停用</option>
                        </select>
                        </li>
                        <li>
                            码商代理：<select name="channelUserId">
                            <option value=""></option>
                            <option th:each="user1:${channels}" th:value="${user1.userId}" th:text="${user1.userName}"></option>
                        </select>
                        </li>

                        <li>
                            <a class="btn btn-success btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-success btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="channel:channelAccount:add">
                <i class="fa fa-plus"></i> 添加
            </a>
            <a class="btn btn-success" onclick="deposit()" shiro:hasPermission="channel:channelAccount:add">
                <i class="fa fa-plus"></i> 预充值
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table" data-mobile-responsive="true"></table>
        </div>
    </div>
</div>
<div th:include="include :: footer"></div>
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('channel:channelAccount:edit')}]];
    var removeFlag = [[${@permission.hasPermi('channel:channelAccount:remove')}]];
    var cashOutFlag = [[${@permission.hasPermi('channel:channelAccountAmount:cashOut')}]];
    var datastatus = [[${@dict.getType('sys_normal_disable')}]];
    var datams = [[${@dict.getType('sys_account_ms')}]];
    var prefix = ctx + "channel/channelAccount";

    $(function () {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            modalName: "通道账户",
            showExport: true,
            sortName: "createTime",
            sortOrder: "desc",
            columns: [{
                checkbox: true
            },
                {
                    field: 'accountId',
                    title: '主键',
                    visible: false
                },
                {
                    field: 'parentId',
                    title: '通道ID',
                    visible: false
                },
                {
                    field: 'channelUserName',
                    title: '码商代理'
                },
                {
                    field: 'account',
                    title: '账号',
                    sortable: true
                },
                {
                    field: 'appid',
                    title: '商家账户(APPID)',
                    sortable: true,
                    visible: false
                },
                {
                    field: 'alias',
                    title: '别名',
                    sortable: true
                },
                {
                    field: 'groupName',
                    title: '分组',
                    sortable: true
                },
                {
                    field: 'payLabel',
                    title: '支付类型'
                },
                {
                    field: 'status',
                    title: '状态',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(datastatus, value);
                    }
                },
                {
                    field: 'amount',
                    title: '剩余金额'
                },
                {
                    field: 'createBy',
                    title: '创建者',
                    visible: false
                },
                {
                    field: 'createTime',
                    title: '创建时间',
                    visible: false
                },
                {
                    field: 'updateBy',
                    title: '修改者',
                    visible: false
                },
                {
                    field: 'updateTime',
                    title: '修改时间',
                    visible: false
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="$.operate.edit(\'' + row.accountId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="doTest(\'' + row.accountId + '\',\''+row.payType+'\')"><i class="fa fa-edit"></i>测试</a> ');
                        actions.push('<a class="btn btn-success btn-xs ' + removeFlag + '" href="#" onclick="$.operate.remove(\'' + row.accountId + '\')"><i class="fa fa-remove"></i>删除</a>');
                        actions.push('&nbsp;<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="deposit(\'' + row.accountId + '\',\''+row.payType+'\')"><i class="fa fa-edit"></i>预充值</a> ');
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="subAccount(\'' + row.accountId + '\')"><i class="fa fa-edit"></i>子账号</a> ');
                        if(cashOutFlag != 'hidden'){
                            actions.push('&nbsp;<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="cashOut(\'' + row.accountId + '\',\''+row.payType+'\')"><i class="fa fa-edit"></i>提现</a> ');
                        }
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    function subAccount(accountId) {
        var url = ctx + "channel/channelAccountSub/" + accountId;
        $.modal.openTab("子账号", url);
    }

    function doTest(account,payType) {
        var payUrl = [[${payUrl}]]
        window.open(payUrl+"?account="+account+"&payType="+payType,"_blank");
    }

    /**
     * 充值
     * @param account
     * @param payType
     */
    function deposit(accountId,payType) {
        layer.open({
            title: '请输入充值金额'
            ,content: '<div><label>请输入金额:&nbsp;&nbsp;</label><input id="amount" type="number" min="0.01" max="50000.00" placeholder="0.00" style="width: 200px"></div>\n' +
                '        <div>\n' +
                '            <label>请选择通道:&nbsp;</label>\n' +
                '            <select  id="payType2"  name="payType2" style="width: 200px;height: 30px">\n' +
                '                <option value="100">支付宝</option>\n' +
                '                <option value="101">云闪付</option>\n' +
                '                <option value="102">微信</option>\n' +
                '                <option value="103">银联扫码</option>\n' +
                '            </select>\n' +
                '        </div>',
            btn: ['充值', '放弃'],
            btn1: function(index, layero){
                var val = $('#amount').val();
                var payType = $('#payType2').val();
                var prefix = ctx + "channel/channelAccountAmount";
                if(val > 0){
                    var userId = [[${userId}]]
                    var data = {
                        payAmount: val,
                        channelUserId:userId
                    }
                    $.ajax({
                        type: "POST",
                        url: prefix + '/order?payType='+payType,
                        contentType: "application/json; charset=utf-8",
                        data: JSON.stringify(data),
                        dataType: "json",
                        success: function (message) {
                            if(message.code== 500){
                                layer.msg(message.msg);
                                layer.close(index);
                            }else{
                                window.open(message.data.payUrl,"_blank");
                            }
                        },
                        error: function (message) {
                            layer.msg(message.msg);
                        }
                    });

                }else{
                    layer.msg('输入的金额必须大于0');
                    return false;
                }
            },
            btn2: function(index, layero){
            }
        });
    }

    function cashOut(accountId) {
        layer.open({
            title: '请输入提现金额'
            ,content: '<div><label>请输入金额:&nbsp;&nbsp;</label><input id="amount" type="number" min="0.01" max="50000.00" placeholder="0.00" style="width: 200px"></div>\n',
            btn: ['充值', '放弃'],
            btn1: function(index, layero){
                var val = $('#amount').val();
                var prefix = ctx + "channel/channelAccountAmount";
                if(val > 0){
                    var data = {
                        amount: val,
                        accountId: accountId
                    }
                    $.ajax({
                        type: "POST",
                        url: prefix + '/cashOut',
                        contentType: "application/json; charset=utf-8",
                        data: JSON.stringify(data),
                        dataType: "json",
                        success: function (message) {
                            if(message.code== 500){
                                layer.msg(message.msg);
                                return false;
                            }else{
                                layer.msg(message.msg);
                                $.table.refresh();
                                layer.close(index);
                            }
                        },
                        error: function (message) {
                            layer.msg(message.msg);
                        }
                    });

                }else{
                    layer.msg('输入的金额必须大于0');
                    return false;
                }
            },
            btn2: function(index, layero){
            }
        });
    }
</script>
</body>
</html>